﻿using Zbk_shop.Models;
using Zbk_shop.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Zbk_shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarController : ControllerBase
    {
        private readonly ICarService _carService;
        /// <summary>
        /// 购物车构造函数 
        /// </summary>
        /// <param name="carService"></param>
        public CarController(ICarService carService)
        {
            //购物车服务类
            _carService = carService;
        }
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddCarForRedis(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //调用购物车服务的方法
            _carService.AddCarForRedis(userId, goodId);
            return "添加购物车成功";
        }
        /// <summary>
        /// 减少购物车商品数量
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DiffCarForRedis(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //减少购物车商品数量
            _carService.DiffCarForRedis(userId, goodId);
            return "减少购物车成功";
        }

        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DeleteCarForRedis(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //删除购物车商品数量
            _carService.DeleteCarForRedis(userId, goodId);
            return "删除购物车成功";
        }
        /// <summary>
        /// 获取购物车商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public GetCarGoodResponse GetCarGoodForRedis()
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var list = _carService.GetCarGoods(userId);
            //返回响应数据
            var response = new GetCarGoodResponse
            {
                CarList = list,
                //每一个商品的数量 *  商品单价 的和
                TotalPrice = list.Sum(x => x.Price * x.Count)
            };
            return response;
        }

        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddCarForSQL(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            //字符串 转int 
            var result = _carService.AddCardForSQL(Convert.ToInt32(userId), goodId);
            if (result)
                return "添加成功";
            return "添加失败";
        }
        /// <summary>
        /// 减少购物车商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DiffCarForSQL(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result = _carService.DiffCarForSQL(Convert.ToInt32(userId), goodId);
            if (result)
                return "减少成功";
            return "减少失败";
        }
        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DeleteCarForSQL(int goodId)
        {
            //获取登录用户的用户Id
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result = _carService.DeleteCarForSQL(Convert.ToInt32(userId), goodId);
            if (result)
                return "删除成功";
            return "删除失败";
        }

        /// <summary>
        /// 获取购物车商品列表
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public GetCarGoodResponse GetCarGoodForSQL()
        {
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var list = _carService.GetCarGoodsForSQL(Convert.ToInt32(userId));

            var response = new GetCarGoodResponse
            {
                CarList = list,
                TotalPrice = list.Sum(x => x.Price * x.Count)
            };
            return response;
        }

    }
}
